﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using EmoneyWeiXinEntity;
using EmoneyWeiXinCommon;

namespace EmoneyWeiXinDAL
{
    /// <summary>
    /// 周年庆转盘活动数据访问类
    /// </summary>
    public class ZhouNianQingZhuanPanDAL
    {

        //插入数据，不管有没有中奖，纪录参与的人员
        public static int InsertData(ZhouNianQingZhuanPanEntity entity)
        {

            string sql = "insert into ZhouNianQingZhuanPan values(@openid,@nickName,@head_Img,@ActName,@gift,@isHaveGift,@isGetGift,GETDATE())";
            SqlParameter[] paras = 
            {
              new SqlParameter("@openid",entity.openid),
              new SqlParameter("@ActName",entity.ActName),
              new SqlParameter("@nickName",entity.nickName==null?"":entity.nickName),
              new SqlParameter("@head_Img",entity.head_Img==null?"":entity.head_Img),
              new SqlParameter("@gift",entity.gift),
              new SqlParameter("@isHaveGift",entity.isHaveGift),
              new SqlParameter("@isGetGift",entity.isGetGift)
            };
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, paras);
        }

        /// <summary>
        /// 获取奖品剩余数量
        /// </summary>
        public static int GetShengyuShuliang()
        {
            string sql = "SELECT top 1 SyCount from ZhouNianqingConfig ";
            return Convert.ToInt32(SQLHelper.ExecuteScalar(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null));
        }
        /// <summary>
        //
        /// </summary>
        /// <returns></returns>
        public static int UpdateShengyuShuliang()
        {
            string sql = "UPDATE ZhouNianqingConfig SET syCount=syCount-1;";
            return Convert.ToInt32(SQLHelper.ExecuteScalar(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null));
        }
        /// <summary>
        /// 是否开启活动
        /// </summary>
        /// <returns></returns>
        public static int ActIsOpen()
        {
            string sql = "SELECT top 1 IsOpen FROM ZhouNianqingConfig";
            return Convert.ToInt32(SQLHelper.ExecuteScalar(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null));
        }

        /// <summary>
        /// 开启和关闭活动
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public static int CloseOrOpen(int i)
        {
            string sql = "UPDATE ZhouNianqingConfig SET IsOpen=" + i;
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null);
        }

        /// <summary>
        /// 设置奖品的剩余个数
        /// </summary>
        /// <returns></returns>
        public static int SetSyCount(int count)
        {

            string sql = "UPDATE ZhouNianqingConfig SET SyCount=@SyCount";
            SqlParameter[] paras = 
            {
              new SqlParameter("@SyCount",count)
            };
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, paras);

        }

        public static ZhouNianQingZhuanPanEntity GetActDataByOpenId(string openid)
        {
            string sql = "SELECT * FROM ZhouNianQingZhuanPan WHERE OPENID=@openid";
            SqlParameter[] paras = 
            {
              new SqlParameter("@openid",openid)
            };
            return SQLHelper.GetDataByColumns<ZhouNianQingZhuanPanEntity>(Config.EmoneyWeiXin, sql, paras);
        }

        /// <summary>
        /// 获取中奖的名单
        /// </summary>
        /// <returns></returns>
        public static List<ZhouNianQingZhuanPanEntity> GetZJMD()
        {
            string sql = "SELECT * FROM ZhouNianQingZhuanPan with(nolock) WHERE isHaveGIft=1";
            return SQLHelper.GetDataByColumnsLst<ZhouNianQingZhuanPanEntity>(Config.EmoneyWeiXin, sql, null);
        }

        public static List<ZhouNianQingZhuanPanEntity> GetAllData()
        {
            string sql = "SELECT * FROM ZhouNianQingZhuanPan";
            return SQLHelper.GetDataByColumnsLst<ZhouNianQingZhuanPanEntity>(Config.EmoneyWeiXin, sql, null);
        }

        public static int DeleteAll()
        {
            string sql = "DELETE ZhouNianQingZhuanPan";
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null);

        }


    }
}
